package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.common.Grideviewmenu;
import cn.com.winning.ecare.utils.Utils;

/**原版九宫格主界面-现停用
 * @author Administrator
 *
 */
public class Grideview extends Activity {
	private GridView gridView;
	private LinearLayout layout;
	// 住院历史记录
	List<Map<String, Object>> list_zy = new ArrayList<Map<String, Object>>();
	private View mShowView;
	private ListView lv_zy;
	private PopupWindow mPopuWin;

	private TextView datechoice;
	private int preId;
	Grideviewmenu gm;
	private Grideview othis = this;
	MyApplication myApplication;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.grideview);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		gm = new Grideviewmenu();
		datechoice = (TextView) findViewById(R.id.datechoice);
		gridView = (GridView) findViewById(R.id.gridview_main);
		layout = (LinearLayout) this.findViewById(R.id.gv_Layout);

		// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
		SimpleAdapter adapter = new SimpleAdapter(this, getData(), // 数据来源
				R.layout.grideview_list,// XML实现
				new String[] { "ItemImage", "ItemText" }, // 动态数组与ImageItem对应的子项
				new int[] { R.id.gridview_imageview, R.id.gridview_textview }// //ImageItem的XML文件里面的一个ImageView,两个TextView
																				// ID
		);
		// 添加并且显示
		gridView.setAdapter(adapter);
		// 添加消息处理
		gridView.setOnItemClickListener(clickListener);
		//获得入院历史记录
		get_zyData();
		//出院日期选择
		datechoice.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//初始化PopWin
				iniPopwin();
				
			}
		});
	}

	private void disPopu() {
		if (null != mPopuWin) {
			mPopuWin.dismiss();
		}
	}

	private void iniPopwin() {
		// TODO Auto-generated method stub
		
		mShowView = LayoutInflater.from(this).inflate(R.layout.floatview_main,
				null);
		lv_zy = (ListView) mShowView.findViewById(R.id.zylist);

		final MyAdapter myAdapter = new MyAdapter(this);
		myAdapter.setList(list_zy);
		lv_zy.setAdapter(myAdapter);

		lv_zy.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				preId = arg2;
				myAdapter.notifyDataSetChanged();

				TextView tv_syxh = (TextView) arg1.findViewById(R.id.syxh);
				TextView tv_ryrq = (TextView) arg1.findViewById(R.id.ryrq);
				MyApplication myApplication = (MyApplication) getApplication();
				myApplication.setSyxh(tv_syxh.getText().toString().trim());
				myApplication.setRyrq(tv_ryrq.getText().toString().trim());
				changeTitle();
				disPopu();
			}
		});

		

		// 将mShowView加入到mPopuWin中6
		mPopuWin = new PopupWindow(mShowView,LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		
		mPopuWin.setBackgroundDrawable(new BitmapDrawable());
		mPopuWin.setTouchable(true);
		mPopuWin.setFocusable(true);
		mPopuWin.setOutsideTouchable(true);
		mPopuWin.setContentView(mShowView);
		mPopuWin.showAsDropDown(datechoice); 

	}

	class MyAdapter extends BaseAdapter {

		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		public List<Map<String, Object>> getList() {
			return list;
		}

		public void setList(List<Map<String, Object>> list) {
			this.list = list;
		}

		public MyAdapter(Context context) {
			mLayoutInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Log.i("aaa", "getView");
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mLayoutInflater.inflate(R.layout.floatview_item,
						null);
				holder = new ViewHolder();
				holder.tv_ryrq = (TextView) convertView.findViewById(R.id.ryrq);
				holder.tv_syxh = (TextView) convertView.findViewById(R.id.syxh);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv_ryrq.setText((CharSequence) list.get(position)
					.get("ryrq"));
			holder.tv_syxh.setText((CharSequence) list.get(position)
					.get("syxh"));
			if (position == preId)
				convertView.setBackgroundColor(Color.parseColor("#232932"));
			else
				convertView.setBackgroundColor(Color.parseColor("#232d3b"));

			return convertView;
		}
	}

	public class ViewHolder {

		public TextView tv_ryrq;
		private TextView tv_syxh;

	}

	

	// 当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
	private OnItemClickListener clickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			Intent intent = null;
			HashMap<String, Object> Item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
			intent = gm.findActivity(intent,Item, othis);
			startActivity(intent);
		}
	};

	private void changeTitle() {
		MyApplication myApplication = (MyApplication) getApplication();
		datechoice.setText("入院日期: "+myApplication.getRyrq().toString().trim());
	}


	// 获取GrideView数据
	private ArrayList<HashMap<String, Object>> getData() {
		MyApplication myApplication = (MyApplication) getApplication();
		return gm.initItem(myApplication);//初始化GrideView
	}

	// 获取住院历史记录
	private void get_zyData() {

		Intent intent = getIntent();

		JSONArray items;
		try {
			items = new JSONArray(intent.getStringExtra("zyinfo"));
			for (int j = 0; j < items.length(); j++) {
				JSONObject jobj = items.getJSONObject(j);
				list_zy.add(Utils.getMap(jobj.toString()));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 初始化首页序号
		MyApplication myApplication = (MyApplication) getApplication();
		if (list_zy.size() > 0) {
			String syxh = (String) list_zy.get(0).get("syxh");
			String ryrq = (String) list_zy.get(0).get("ryrq");
			String hzxm = (String) list_zy.get(0).get("hzxm");
			myApplication.setSyxh(syxh);
			myApplication.setRyrq(ryrq);
			myApplication.setHzxm(hzxm);
		} else {
			myApplication.setSyxh("");
			myApplication.setRyrq("");
			myApplication.setHzxm("");
		}
		datechoice.setText("入院日期: "+myApplication.getRyrq().toString().trim());
	}
}
